package com.msmall.community.roster.dao.hibernate;

import java.util.List;

import com.msmall.base.dao.hibernate.BaseDaoHibernate;
import com.msmall.community.roster.dao.RosterGroupDao;
import com.msmall.community.roster.model.RosterGroup;

public class RosterGroupDaoHibernate extends BaseDaoHibernate<RosterGroup,Long> implements RosterGroupDao{
	
	public RosterGroupDaoHibernate(){
		super(RosterGroup.class);
	}
	/**
	 * 获得用户的所有组
	 */
	@SuppressWarnings("unchecked")
	public List<RosterGroup> getGroups(Long userid) throws Exception{
		try{
			String queryString = "from RosterGroup g where g.userid=?";
			return getHibernateTemplate().find(queryString, new Object[]{userid});
		}catch(Exception e){
			throw e;
		}
	}
	/**
	 * 获得用户的组,排除groupNames: 以逗号分隔的组名, eg:陌生人,黑名单
	 */
	public List<RosterGroup> getRosterGroupLimit(Long userid, String groupNames) throws Exception{
		try{
			if(groupNames == null || "".equals(groupNames.trim())){
				return getGroups(userid);
			}
			String params = "";
			String names[] = groupNames.split(",");
			for(String name:names){
				name = name.trim();
				params += ("'"+name+"'"+",");
			}
			params = params.substring(0, params.length()-1);
			String queryString = "from RosterGroup g where g.userid="+userid+" and g.name not in ("+params+")";
			return getHibernateTemplate().find(queryString);
		}catch(Exception e){
			throw e;
		}
	}
	/**
	 * 获得用户的组
	 */
	@SuppressWarnings("unchecked")
	public RosterGroup getGroup(Long userid, String groupName) throws Exception{
		try{
			String queryString = "from RosterGroup g where g.userid=? and g.name=?";
			List<RosterGroup> list = getHibernateTemplate().find(queryString, new Object[]{userid, groupName});
			if(list != null && list.size()>0)
				return list.get(0);
			return null;
		}catch(Exception e){
			throw e;
		}
	}
}
